package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.DepartmentDao;
import cn.edu.sdjzu.xg.bysj.dao.StudentClassDao;
import cn.edu.sdjzu.xg.bysj.domain.Department;
import cn.edu.sdjzu.xg.bysj.domain.StudentClass;
import cn.edu.sdjzu.xg.bysj.exception.BysjException;

import java.sql.SQLException;
import java.util.Collection;

public final class StudentClassService {
    private static StudentClassDao studentClassDao = StudentClassDao.getInstance();
    private static StudentClassService studentClassService = new StudentClassService();

    private StudentClassService() {
    }

    public static StudentClassService getInstance() {
        return studentClassService;
    }

    public Collection<StudentClass> findAll() throws SQLException, BysjException {
        return studentClassDao.findAll();
    }

//    public Collection<StudentClass> getAll(School school) throws SQLException {
//        Collection<StudentClass> studentClasss = new HashSet<StudentClass>();
//        for(StudentClass studentClass: studentClassDao.findAll()){
//            if(studentClass.getSchool()==school){
//                studentClasss.add(studentClass);
//            }
//        }
//        return studentClasss;
//    }

    public StudentClass find(Integer id) throws SQLException, BysjException {
        return studentClassDao.find(id);
    }

    public boolean update(StudentClass studentClass) throws SQLException {
        return studentClassDao.update(studentClass);
    }

    public boolean add(StudentClass studentClass) throws SQLException {
        return studentClassDao.add(studentClass);
    }

    public boolean delete(Integer id) throws SQLException, BysjException {
        StudentClass studentClass = this.find(id);
        return studentClassDao.delete(studentClass);
    }

    public boolean delete(StudentClass studentClass) throws SQLException {
        return studentClassDao.delete(studentClass);
    }

    public static void main(String[] args) throws SQLException, BysjException {
        //从数据库中读取id=2的学院记录，转换为内存对象
        Department mgtSch = DepartmentDao.getInstance().find(2);
        System.out.println(mgtSch);
        //在内存中创建misDept和pmDept对象
        StudentClass misDept = new StudentClass("信息191", "02021", mgtSch);
        StudentClass pmDept = new StudentClass("信管192", "02012", mgtSch);
        //在内存中令两个对象和mgtSch建立关联关系
        pmDept.setDepartment(mgtSch);
        misDept.setDepartment(mgtSch);
        //将内存对象pmDept和misDept保存到数据库表中
        StudentClassDao.getInstance().add(pmDept);
        StudentClassDao.getInstance().add(misDept);

        misDept = StudentClassService.getInstance().find(1);
        System.out.println(misDept);

        StudentClassDao.getInstance().delete(4);
        Collection<StudentClass> studentClasss = StudentClassDao.getInstance().findAll();
        System.out.println(studentClasss);

        int count_by_department_id = StudentClassDao.getInstance().count(mgtSch);
        System.out.println(count_by_department_id);
    }

}

